//小鸟类
//是循环渲染三只小鸟
//其实是循环渲染图片的三个部分
import {Sprite} from "../base/Sprite.js";
import {DataStore} from "../base/DataStore.js";
const rpx=DataStore.getRpx()
const screenWidth    = window.innerWidth
const screenHeight   = window.innerHeight
export class Birds extends Sprite {
    constructor(man) {
        const image = Sprite.getImage(man);
        // super(image, 9, 10, 34, 24,
        //     0, 0, image.width, image.height);
        super(
          image,
            0, 0,
            image.width, image.height,
            screenWidth/2-image.width/3*rpx+38*rpx ,260*rpx,
            image.width*2/3*rpx, image.height*2/3*rpx
        )

        const birdX = DataStore.getInstance().canvas.width / 4;
        this.birdsX = birdX
        // const birdY = DataStore.getInstance().canvas.height / 2;
        const birdY = DataStore.getInstance().canvas.height / 1.2-image.height*2/3*rpx;
        this.ground=birdY//地板高度
        this.birdsY = birdY

        this.y = birdY;
        // this.index = 0;
        // this.count = 0;
        this.time = 0;

        // 用于在手指移动的时候标识手指是否已经在飞机上了
        this.touched = false
        this.x1=birdX;
        this.x2=birdX;
        this.jumpHeightTotal=0;//要跳的总高度
        this.jumpHeight=0; //每帧跳多高
        this.finish=true;//判断是否上下跳动了一次
        this.startSpeed=20;//初始速度
        this.currencySpeed=20

    }
    checkIsFingerOnAir(x, y) {//判断是否在人物上
        const deviation = 30
    
        return !!(   x >= this.birdsX - deviation
                  && y >= this.birdsY - deviation
                  && x <= this.birdsX + this.width + deviation
                  && y <= this.birdsY + this.height + deviation  )
      }
    
    //   * 根据手指的位置设置飞机的位置
    //   * 保证手指处于飞机中间
    //   * 同时限定飞机的活动范围限制在屏幕中
     setAirPosAcrossFingerPosZ(x, y) {
       let disX = x - this.width / 2
       let disY = y - this.height / 2
   
       if ( disX < 0 )
         disX = 0
   
       else if ( disX > screenWidth - this.width )
         disX = screenWidth - 34
   
       if ( disY <= 0 )
         disY = 0
   
       else if ( disY > screenHeight - this.height )
         disY = screenHeight - 24
   
        this.birdsX = disX
       this.draw()
     }


    //  * 玩家响应手指的触摸事件
    //  * 改变战机的位置
    //  */
    timer(){
      setInterval(()=>{
        this.time++
      },400)
    }
    initEvent() {

        DataStore.getInstance().canvas.addEventListener('touchstart', ((e) => {
          this.timer()
        e.preventDefault()
        console.log('start')
        let x = e.touches[0].clientX
        let y = e.touches[0].clientY
        
        //
        if ( this.checkIsFingerOnAir(x, y) ) {
          this.touched = true
          this.x1=x   
          this.setAirPosAcrossFingerPosZ(x, y)
        }
  
      }))
  
      DataStore.getInstance().canvas.addEventListener('touchmove', ((e) => {
        e.preventDefault()
        
        let x = e.touches[0].clientX
        let y = e.touches[0].clientY
        if ( this.touched )
          this.setAirPosAcrossFingerPosZ(x, y)
  
      }))
  
      DataStore.getInstance().canvas.addEventListener('touchend', ((e) => {
        e.preventDefault()
        let x = e.changedTouches[0].clientX
        let y = e.changedTouches[0].clientY
        this.x2=x
        if(Math.abs(this.x1-this.x2)<5){
          // //模拟重力加速度
        const g = 0.98 / 5;
        let offsetY = Math.floor((g * this.time * this.time ) +60)
        // let offsetY=this.time*10+
        if(offsetY>screenHeight){
          offsetY=screenHeight
        }
        // console.log(offsetY)
        if(this.finish==true){
          this.jumpHeight=0
          this.jumpHeightTotal=offsetY
        }
        }
        console.log(this.finish)
        
        this.time=0
        this.touched = false
      }))
    }

    draw() {
        //切换三只小鸟的速度
        // const speed = 0.2;
        // this.count = this.count + speed;
        // //0,1,2
        // if (this.index >= 2) {
        //     this.count = 0;
        // }
        // //减速器的作用
        // this.index = Math.floor(this.count);

        // //模拟重力加速度
        // const g = 0.98 / 2.4;
        // //向上移动一丢丢的偏移量
        // const offsetUp = 30;
        // //小鸟的位移
        // const offsetY = (g * this.time * (this.time - offsetUp)) / 2;

        // for (let i = 0; i <= 2; i++) {
        //     this.birdsY[i] = this.y[i] + offsetY;
        // }

        // for (let i = 0; i <= 2; i++) {
        //     this.birdsX[i] = this.x[i] + 10;
        // }
        // this.time++;
        
        if(this.jumpHeightTotal!=0){

          if(this.jumpHeight<this.jumpHeightTotal ){//往上跳
            this.isUp=true
            this.jumpHeight+=1;
              this.currencySpeed-=0.7
            // console.log(this.birdsY,)
            this.birdsY -=this.currencySpeed
            if(this.birdsY>this.ground){//回到底部 重置数据
              this.jumpHeightTotal=0;
              this.jumpHeight=0
              this.finish=true
              this.currencySpeed=20
              this.birdsY =this.ground
            }

            super.draw(
              this.img,
              this.clippingX, this.clippingY,
              this.clippingWidth, this.clippingHeight,
              this.birdsX,this.birdsY,
              this.birdsWidth, this.birdsHeight
          );
          }
        }else{
          super.draw(
            this.img,
            this.clippingX, this.clippingY,
            this.clippingWidth, this.clippingHeight,
            this.birdsX, this.birdsY,
            this.birdsWidth, this.birdsHeight
        );
        }
        
       
    }


}